﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Web;

namespace MainService
{
    [DataContract]
    public class DatabaseDetails
    {
        [DataMember]
        public int DbId { get; set; }

        [DataMember]
        public string DBName { get; set; }

        [DataMember]
        public int CreatedBy { get; set; }

        [DataMember]
        public bool IsCaseSensitiveValidation { get; set; }

        [DataMember]
        public int RowCount { get; set; }

        [DataMember]
        public int MyProperty { get; set; }

        [DataMember]
        public Dictionary<int, string> GetServiceDetails { get; set; }

        [DataMember]
        public DateTime LastUpdated { get; set; }

        [DataMember]
        public string UserMessage { get; set; }

        public List<int> ServiceDetails { get; set; }

        public List<ScanData> ScanDataList { get; set; }

        internal List<ScanData> GetScanDataList(byte[] lstData)
        {
            BinaryFormatter bformatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            ScanData _data = null;
            bformatter = new BinaryFormatter();
            stream = new MemoryStream(lstData);
            DataTable dt = (DataTable)bformatter.Deserialize(stream);
            stream.Close();
            List<ScanData> lst = new List<ScanData>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                _data = new ScanData();
                _data.BarcodeVale = Convert.ToString(dt.Rows[i][0]);
                _data.BarcodeResponse = Convert.ToString(dt.Rows[i][1]);
                _data.Validity = Convert.ToInt32(dt.Rows[i][2]);
                lst.Add(_data);
            }

            return lst;
        }
        public static byte[] StrToByteArray(string str)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            return encoding.GetBytes(str);
        }
        public static string ByteArrayToStr(byte[] barr)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            return encoding.GetString(barr, 0, barr.Length);
        }
        internal string CreateDatabase(ref int nwdbId)
        {
            DatabaseOperations _dbOp = new DatabaseOperations();
            return _dbOp.CreateNewDatabase(DBName, IsCaseSensitiveValidation, CreatedBy, ScanDataList, ServiceDetails, ref nwdbId);
        }
        internal string AddScanData()
        {
            DatabaseOperations _dbOp = new DatabaseOperations();
            return _dbOp.AddScanData(CreatedBy, DbId, ScanDataList[0]);
        }
        internal string RenameDB()
        {
            DatabaseOperations _dbOp = new DatabaseOperations();
            return _dbOp.RenameDatabase(CreatedBy, DbId, DBName);
        }
        internal string DeteleDB()
        {
            DatabaseOperations _dbOp = new DatabaseOperations();
            return _dbOp.DeleteDatabase(CreatedBy, DbId);
        }
        internal DatabaseDetails GetDatabaseDetails()
        {
            DatabaseOperations _dbOp = new DatabaseOperations();
            return _dbOp.GetDatabaseDetails(CreatedBy, DbId);
        }
        internal string AssociateServiceToDb()
        {
            DatabaseOperations _dbOp = new DatabaseOperations();
            return _dbOp.AssociateServicToDatabase(DbId, ServiceDetails, CreatedBy);
        }
        internal List<int> GetDBIDList(int clientId)
        {
            DatabaseOperations op = new DatabaseOperations();
            return op.GetDBIDList(clientId);
        }
        internal string GetTableData()
        {
            DatabaseOperations op = new DatabaseOperations();          
            
            return DataTableToCSV(op.GetDBIDList(DbId, CreatedBy));
          
        }
        public static string DataTableToCSV(DataTable table)
        {
            var result = new StringBuilder();
            for (int i = 0; i < table.Columns.Count; i++)
            {
                result.Append(table.Columns[i].ColumnName);
                result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
            }

            foreach (DataRow row in table.Rows)
            {
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    result.Append(row[i].ToString());
                    result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
                }
            }

            return result.ToString();
        }

    }

    public class ScanData
    {
        public string BarcodeVale { get; set; }
        public string BarcodeResponse { get; set; }
        public int Validity { get; set; }
    }
}